import { JXDef } from "../../../conventions/JXCommon";
import { GCtrl } from "../../../Core/GCtrl";
import GViewBase from "../../../Core/GView/GViewBase";
import { AudioMgr } from "../../../Core/Manager/AudioMgr";
import { CMsg, CURRENCY, ITEM_TYPE, PILL, PointInfo, SPECIALWINDOW } from "../../Common/Define";
import { L } from "../../Common/Language";
import { VIEW_ID } from "../../Common/UI";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
const { ccclass, property, menu } = cc._decorator;
@ccclass @menu('View/Common/CurrencyFrame')
export default class CurrencyFrame extends GViewBase {

    @property({ type: cc.Label, tooltip: "文案" }) lab: cc.Label = null;
    @property({ type: cc.Sprite, tooltip: "图标" }) icon: cc.Sprite = null;
    @property({ type: cc.Node, tooltip: "按钮" }) btn: cc.Node = null;
    @property({ type: cc.Node, tooltip: "ad按钮" }) videoBtn: cc.Node = null;

    @property({ type: cc.Node, tooltip: "ad按钮图片" }) videoImg: cc.Node = null;
    @property({ type: cc.Node, tooltip: "ad按钮文案" }) videoLab: cc.Node = null;

    @property(cc.Sprite) heroChip: cc.Sprite = null;
    text: string = 'hello';

    /**数据 */
    private _data: RJXItem;
    /** */
    private _costData: RJXItem;
    /**是否观看视频 */
    private _isWatch: boolean = false;
    /**奖励数量 */
    private _videoNum: number = null;
    /** */
    private _equipId: string = null;
    private _del: boolean = false;
    onGStart(cost: any[] | RJXItem, times: number = 1, del = false, equip?: string) {
        this._del = del;
        if (del) {
            this.del(cost);
            this._equipId = equip;
        } else {
            if (cost[0] == SPECIALWINDOW.VIDEO) {
                this.videoInit();
            } else if (cost[0] == SPECIALWINDOW.HELP) {
                this.helpInit(cost);
            } else {
                this.init(cost);
            }
        }
    }

    protected helpInit(cost: any[] | RJXItem) {
        this.btn.active = false;
        this.videoBtn.active = false;
        let str = GameMgr.heroData.getRaw<SHeroDataRaw>(cost[1]).tale;
        this.lab.string = str;
    }

    protected videoInit() {
        let str = L(JXLocales.currencyLab["info" + SPECIALWINDOW.VIDEO]);
        this.lab.string = str;
    }

    init(cost) {
        let tpl = cost instanceof Array ? RJXItem.tpl(...cost) : cost;
        let item = GameMgr.RBagData.getMegreItems(tpl.id, tpl.type);
        let officerLv = GameMgr.lUserData.officerLv;
        this._data = item;
        // console.log('通用框', item);
        switch (this._data.type) {
            case ITEM_TYPE.CURRENCY:
                {
                    switch (this._data.id) {
                        case CURRENCY.GOLD:
                            {
                                let num = 0;
                                num = GameMgr.officerData.getTv(officerLv, 0);
                                this._videoNum = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.videoAwardsGold);
                                this._videoNum += num;
                                break;
                            }
                        case CURRENCY.YUSHI:
                            {
                                let num = 0;
                                num = GameMgr.officerData.getTv(officerLv, 1);
                                this._videoNum = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.videoAwardsGem);
                                this._videoNum += num;
                                break;
                            }
                        case CURRENCY.WHEAT:
                            {
                                this._videoNum = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.forageUp);
                                this.icon.node.scale = 2
                                break;
                            }
                        default:
                            break;
                    }
                    let str = L(JXLocales.currencyLab["info" + this._data.id], this._videoNum);
                    this.lab.string = str;

                    this.assetImpl.spriteAtlasFrame(this.icon, Res.texture.views.icon, "icon_" + this._data.id);
                    break;
                }
            case ITEM_TYPE.PROP:
                {
                    if (this._data.id >= PILL.PILL0 && this._data.id <= PILL.PILL5) {
                        this.assetImpl.spriteAtlasFrame(this.icon, Res.texture.views.icon, "icon_201");
                        let str = L(JXLocales.currencyLab["info" + 201], 1);
                        this._videoNum = 1;
                        this.lab.string = str;
                    }
                    if (this._data.id >= 1001 && this._data.id <= 2000) {
                        this._videoNum = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.heroTv);
                        let str = L(JXLocales.currencyLab["info" + 1001], this._videoNum);
                        this.lab.string = str;
                        this.assetImpl.spriteAtlasFrame(this.icon, Res.texture.views.icon, "icon_plane");
                        this.assetImpl.spriteFrame(this.heroChip, Res.texture.hero.hero + this._data.id);
                    }
                    break;
                }
            case ITEM_TYPE.EQUIPDATA:
                {
                    break;
                }
            case ITEM_TYPE.HERO:
                {
                    break;
                }
            default:
                break;
        }
    }

    del(cost) {
        let tpl = cost instanceof Array ? RJXItem.tpl(...cost) : cost;
        // console.log(cost, 'tpl', tpl);
        this._costData = cost;
        // console.log('tpl', this._costData);
        this.videoImg.active = false;
        this.videoLab.x = 0;
        let num = 0;
        let officerLv = GameMgr.lUserData.officerLv;
        num = GameMgr.officerData.getTv(officerLv, 1);
        this._videoNum = cost[2];
        // this._videoNum += num;
        let str = L(JXLocales.currencyLab["del" + cost[0]], this._videoNum);
        this.lab.string = str;
        this.assetImpl.spriteAtlasFrame(this.icon, Res.texture.views.icon, "icon_" + cost[0]);
    }

    start() {


    }

    /**点击观看领取 */
    adBtn() {
        if (this._isWatch) {
            return;
        }
        if (!this.videoImg.active) {
            GCtrl.ES.emit(CMsg.client.equip.ondelEquip, this._equipId);
            // console.log('点击观看领取', this._costData);
            GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, [this._costData]);
            this.close();
        } else {
            this._isWatch = true;
            GameMgr.sdkMgr.watchAd(() => {

                this._isWatch = false;
                if (this._videoNum != null) {
                    if (this._data.id === CURRENCY.YUSHI) {
                        GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.common, { msg: PointInfo.common_ad_yushi })
                    } else if (this._data.id === CURRENCY.GOLD) {
                        GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.common, { msg: PointInfo.common_ad_coin })
                    }
                    if (this._data.type == ITEM_TYPE.PROP && this._data.id >= 1001 && this._data.id <= 2000) {
                        {
                            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.common, { msg: PointInfo.common_ad_chip })
                        }
                    }
                    1
                    if (this._data.type == ITEM_TYPE.PROP && this._data.id >= PILL.PILL0 && this._data.id <= PILL.PILL5) {
                        GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.common, { msg: PointInfo.common_ad_pill })
                    }

                    if (this._data.id === CURRENCY.WHEAT) {
                        GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.common, { msg: PointInfo.common_ad_wheat })
                    }

                    // GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, [[this._data.id, this._data.type, (this._videoNum)]])
                    GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, [[this._data.id, this._data.type, (this._videoNum)]]);

                } else {
                    GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.VipCtrl, { msg: PointInfo.vip_btn })
                }
                this.close();
            }, () => {
                this._isWatch = false;
            })
        }
    }
    close() {
        if (this._del) {
            AudioMgr.Ins().playEffect(Res.audio.coin);
        }
        this.onClose();
    }
}
